package com.zookeeper;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

/**
 * Created by dong peng on 2017/12/15.
 *
 * 借助zk做分布式锁思路
 * 写锁，可借助节点创建是唯一的，谁创建成功，谁持有锁，用完再删除
 * 读锁，可借助有序节点，获取所有子节点，序号最小的节点作为持有锁
 *
 */
public class ZkClientSample {

    private static ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183",5000);

    @Test
    public void getCreateTime() {
        // 获取创建时间
        long createtime =  zkClient.getCreationTime("/zkClient");
        System.out.println(createtime);

        //获取节点状态
        Stat stat = new Stat();
        zkClient.readData("/zkClient",stat);
        System.out.println("节点状态值："+stat);

    }

    @Test
    public void createTmpNode() {
        // 创建临时节点
        String createPath =zkClient.create("/zkClient","hello", CreateMode.EPHEMERAL);
        System.out.println(createPath);

    }

    @Test
    public void createSeqNode() {
        // 创建有序临时节点
        for(int i = 0; i < 10; i++){
            String createPath =zkClient.create("/zkClient/","hello", CreateMode.EPHEMERAL_SEQUENTIAL);
            System.out.println(createPath);
        }


    }

    @Test
    public void testWatcher() {
        boolean flag = zkClient.exists("/zkClient");
        if(flag) {
            zkClient.create("/zkClient/0","001",CreateMode.EPHEMERAL);
        }else {
            zkClient.create("/zkClient","hello",CreateMode.PERSISTENT);
            zkClient.create("/zkClient/0","001",CreateMode.EPHEMERAL);
        }

        // 对子节点做数据监听
        zkClient.subscribeDataChanges("/zkClient/0", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println("监听数据改变");
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println("监听数据删除");
            }
        });


        zkClient.writeData("/zkClient/0","aaa");


        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void existsNode() {
        boolean flag = zkClient.exists("/zkClient");
        if(flag) {
            System.out.println("存在该节点");
        }else {
            System.out.println("不存在");
        }
    }

    public static void main(String[] args) {
        System.out.println("zookeeper会话已经建立");

        for(int i = 0; i < 5 ; i++)
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String createPath =zkClient.create("/zkClient/0","hello", CreateMode.EPHEMERAL);
                    System.out.println(createPath);
                }
            }).start();

    }
}
